package cn.yuemouren.tree.segment.leetcode;

import cn.yuemouren.tree.segment.SegmentMerge;
import cn.yuemouren.tree.segment.SegmentTree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: Timi
 * @Description: TODO
 * @Date: 2020/7/8 22:43
 * @Version: v1.0
 */
public class ValidParenthesis_303_1 {

    private SegmentTree<Integer> segmentTree = null;

    public ValidParenthesis_303_1(int[] nums) {
        if(nums.length > 0){
            Integer[] data = new Integer[nums.length];
            for (int i = 0; i < data.length; i++)
                data[i] = nums[i];
            segmentTree = new SegmentTree<Integer>(data, new SegmentMerge<Integer>() {
                @Override
                public Integer merge(Integer t1, Integer t2) {
                    return t1 + t2;
                }
            });
        }
    }

    public int sumRange(int i, int j) {
        if(segmentTree == null)
            throw new IllegalArgumentException("nums is null.");
        return segmentTree.query(i,j);
    }

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        ValidParenthesis_303_1 dto = new ValidParenthesis_303_1(nums);
        int i = dto.sumRange(0, 5);
        System.out.println(i);
    }
}
